package com.keba.lamp.tenant.controller;

import cn.hutool.core.util.StrUtil;
import com.keba.basic.base.R;
import com.keba.basic.context.ContextUtil;
import com.keba.basic.database.mybatis.conditions.Wraps;
import com.keba.basic.interfaces.echo.EchoService;
import com.keba.basic.utils.BeanPlusUtil;
import com.keba.lamp.common.constant.AppendixType;
import com.keba.lamp.common.constant.BizConstant;
import com.keba.lamp.file.entity.Appendix;
import com.keba.lamp.file.service.AppendixService;
import com.keba.lamp.system.entity.system.DefParameter;
import com.keba.lamp.system.service.system.DefParameterService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.keba.basic.base.controller.SuperController;
import com.keba.lamp.tenant.service.DefTenantAppConfigService;
import com.keba.lamp.tenant.entity.DefTenantAppConfig;
import com.keba.lamp.tenant.vo.save.DefTenantAppConfigSaveVO;
import com.keba.lamp.tenant.vo.update.DefTenantAppConfigUpdateVO;
import com.keba.lamp.tenant.vo.result.DefTenantAppConfigResultVO;
import com.keba.lamp.tenant.vo.query.DefTenantAppConfigPageQuery;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * 租户app配置
 * </p>
 *
 * @author twg
 * @date 2025-06-15 18:10:20
 * @create [2025-06-15 18:10:20] [twg] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/defTenantAppConfig")
@Tag(name = "租户app配置")
public class DefTenantAppConfigController extends SuperController<DefTenantAppConfigService, Long, DefTenantAppConfig
        , DefTenantAppConfigSaveVO, DefTenantAppConfigUpdateVO, DefTenantAppConfigPageQuery, DefTenantAppConfigResultVO> {
    private final EchoService echoService;
    private final DefParameterService defParameterService;
    private final AppendixService appendixService;

    @Override
    public EchoService getEchoService() {
        return echoService;
    }

    @Override
    public R<DefTenantAppConfigResultVO> get(Long aLong) {
        DefTenantAppConfig entity = superService.getById(aLong);
        DefTenantAppConfigResultVO defTenantAppConfigResultVO = BeanPlusUtil.toBean(entity, getResultVOClass());
        DefParameter defParameter = defParameterService.getSuperManager().getOne(Wraps.<DefParameter>lbQ().eq(DefParameter::getKey, BizConstant.SHARE_LINK_PREFIX));
        if (Objects.nonNull(defParameter)) {
            defTenantAppConfigResultVO.setShareUrl(StrUtil.format("{}?appId={}", defParameter.getValue(), defTenantAppConfigResultVO.getAppId()));
        }
        List<Appendix> appendixList = appendixService.list(Wraps.<Appendix>lbQ().eq(Appendix::getBizId, defTenantAppConfigResultVO.getId()));
        defTenantAppConfigResultVO.setH5LogoFileId(appendixList.stream().filter(item -> AppendixType.System.DEF__H5__LOGO.equals(item.getBizType())).findFirst().map(Appendix::getId).orElse(null))
                .setMpLogoFileId(appendixList.stream().filter(item -> AppendixType.System.DEF__MP__LOGO.equals(item.getBizType())).findFirst().map(Appendix::getId).orElse(null));
        return success(defTenantAppConfigResultVO);
    }

    @Override
    public R<DefTenantAppConfigResultVO> getDetail(Long aLong) {
        return get(aLong);
    }


    @GetMapping
    public R<DefTenantAppConfigResultVO> getByTenantId() {
        DefTenantAppConfigResultVO defTenantAppConfigResultVO = BeanPlusUtil.toBean(superService.getByTenantId(), getResultVOClass());
        DefParameter defParameter = defParameterService.getSuperManager().getOne(Wraps.<DefParameter>lbQ().eq(DefParameter::getKey, BizConstant.SHARE_LINK_PREFIX));
        if (Objects.nonNull(defParameter)) {
            defTenantAppConfigResultVO.setShareUrl(StrUtil.format("{}?appId={}", defParameter.getValue(), defTenantAppConfigResultVO.getAppId()));
        }
        List<Appendix> appendixList = appendixService.list(Wraps.<Appendix>lbQ().eq(Appendix::getBizId, defTenantAppConfigResultVO.getId()));
        defTenantAppConfigResultVO.setH5LogoFileId(appendixList.stream().filter(item -> AppendixType.System.DEF__H5__LOGO.equals(item.getBizType())).findFirst().map(Appendix::getId).orElse(null))
                .setMpLogoFileId(appendixList.stream().filter(item -> AppendixType.System.DEF__MP__LOGO.equals(item.getBizType())).findFirst().map(Appendix::getId).orElse(null));
        return success(defTenantAppConfigResultVO);
    }
}


